<?php
/**
 * 后台用控制器模板
 */
namespace app\admin\controller\supervision;
use app\common\model as M;
use app\admin\controller\AuthController;
use app\admin\model\user\User;
use app\common\model\Organ;
use service\TencentMapService;
use service\JsonService;

class School extends AuthController
{
	public $model = '\app\common\model\School';

    use \traits\ShotcutControllerTrait;

    /**
     * [_add description]
     * @Author   FW
     * @DateTime 2021-05-08
     * @Email    123763712@qq.com
     */
    protected function _save( $data )
    {
        if ( isset($data['area']) && is_array($data['area']) ) {
            if ( count($data['area']) == 3 ) {
                list($data['province'] , $data['city'] , $data['distinct']) = $data['area'];

                if ( isset($data['area_show']) && isset($data['address']) ) {
                    $geocoder = TencentMapService::geocoder($data['area_show'] . $data['address']);
                    $data['lng'] = $geocoder['lng'];
                    $data['lat'] = $geocoder['lat'];
                }
            }
        }
    	return $data;
    }

    /**
     * [_index description]
     * @Author   FW
     * @DateTime 2021-05-08
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    protected function _index()
    {
    	$this->assign([
    		'organs'     =>    Organ::getListArr(),
            'area_trees' =>    json_encode(\app\common\model\Area::toTree() , JSON_UNESCAPED_UNICODE)
    	]);
    }

    /**
     * 默认佣金设置
     * @Author   FW
     * @DateTime 2021-09-08
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function default_commission ()
    {
        $school_id = (int)$this->request->get('school_id');

        return JsonService::successful('successful' , M\SchoolDefaultCommission::default_commission($school_id));
    }

    /**
     * 保存默认佣金设置
     * @Author   FW
     * @DateTime 2021-09-08
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function save_default_commission ()
    {
        $post = $this->request->post();

        if ( !isset($post['org_id']) || $post['org_id'] == false ) {
            $post['org_id'] = $this->model::getBelongOrganId();
        }

        if ( !isset($post['school_id']) || $post['school_id'] == false ) {
            $post['school_id'] = $this->model::getBelongSchoolId();
        }

        if ( !isset($post['default_commission']) || !is_array($post['default_commission']) ) {
            return JsonService::fail('缺少设置项');
        }

        $res = M\SchoolDefaultCommission::save_default_commission($post);
        if ( $res !== false ) return JsonService::successful('保存成功');

        return JsonService::fail('操作失败');
    }
} //Class End
